package com.jintoufs.service.role.impl;

import com.google.common.collect.Maps;
import com.jintoufs.dao.role.RoleDAO;
import com.jintoufs.domain.role.Role;
import com.jintoufs.logAnnotation.SystemServiceLog;
import com.jintoufs.service.role.RoleService;
import com.jintoufs.service.security.AuthorizationService;
import com.jintoufs.util.ztree.TreeNode;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "roleService")
public class RoleServiceImpl implements RoleService {

    @Resource
    private RoleDAO roleDAO;

    @Resource
    private AuthorizationService authorizationService;

    @Override
    @SystemServiceLog(description = "新增角色")
    public AjaxMsg add(Role entity, String resourceIds) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        ajaxMsg.setCode(AjaxMsg.SUCCESS);
        //验证名称唯一性
        if (entity != null) {
            Map<String, Object> params = Maps.newHashMap();
            params.put("roleName", entity.getRoleName());
            if (roleDAO.getCountByParams(params) > 0) {
                ajaxMsg.setCode(AjaxMsg.FAILURE);
                ajaxMsg.setMessage("角色名称已经存在！");
                return ajaxMsg;
            }
        }
        Integer id = roleDAO.insert(entity);
        if (id == 0) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("新增失败");
        } else {
            authorizationService.grant(entity.getId(), resourceIds);
        }
        return ajaxMsg;
    }

    @Override
    @SystemServiceLog(description = "修改角色信息")
    public AjaxMsg update(Role entity, String resourceIds) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        ajaxMsg.setCode(AjaxMsg.SUCCESS);
        //验证名称唯一性
        Role role = roleDAO.queryById(entity.getId());
        if (entity.getRoleName() != null && !entity.getRoleName().equals(role.getRoleName())) {
            Map<String, Object> params = Maps.newHashMap();
            params.put("roleName", entity.getRoleName());
            if (roleDAO.getCountByParams(params) > 0) {
                ajaxMsg.setCode(AjaxMsg.FAILURE);
                ajaxMsg.setMessage("角色名称已经存在！");
                return ajaxMsg;
            }
        }
        boolean flag = roleDAO.update(entity);
        if (flag) {
            authorizationService.grant(entity.getId(), resourceIds);
            ajaxMsg.setMessage("编辑成功");
        } else {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("编辑失败");
        }
        return ajaxMsg;
    }

    @Override
    @SystemServiceLog(description = "删除角色")
    public void delete(Integer id) {
        roleDAO.delete(id);
    }

    @Override
    @SystemServiceLog(description = "分页获取角色列表")
    public List<Role> getByFilter(Map<String, Object> params) {
        return roleDAO.getByFilter(params);
    }

    @Override
    public List<TreeNode> getRoleMenu(String roleId) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<Role> list = roleDAO.getByFilter(params);
        List<TreeNode> nodes = new ArrayList<TreeNode>();
        /* 根据网点OID构建数据时,只需要两级即可(网点及其直接上级区域) */
        for (Role tmp : list) {
            TreeNode areaNode = new TreeNode();
            areaNode.setId(String.valueOf(tmp.getId()));
            areaNode.setName(tmp.getRoleName());
            if (!"".equals(roleId) && Integer.parseInt(roleId) == tmp.getId()) {
                areaNode.setChecked(true);
            }
            nodes.add(areaNode);
        }
        return nodes;
    }
}
